{% extends "zato/index.html" %}

{% block html_title %}Pub/sub subscriptions {% if topic_id %}for topic {{ topic_name }}{% endif %}{% endblock %}

{% block "extra_css" %}
    <link rel="stylesheet" type="text/css" href="/static/css/jquery.alerts.css">
    <link rel="stylesheet" type="text/css" href="/static/css/multi-select.css">
    <style type="text/css">
        .ui-dialog-titlebar {
            padding-bottom:10px !important;
        }
    </style>
{% endblock %}

{% block "extra_js" %}

    <script nonce="{{ CSP_NONCE }}" type="text/javascript" src="/static/jquery/jquery.tablesorter.min.js"></script>
    <script nonce="{{ CSP_NONCE }}" type="text/javascript" src="/static/jquery/jquery.cookie.js"></script>
    <script nonce="{{ CSP_NONCE }}" type="text/javascript" src="/static/jquery/jquery.alerts.min.js"></script>

    <script nonce="{{ CSP_NONCE }}" type="text/javascript" src="/static/js/common.js"></script>
    <script nonce="{{ CSP_NONCE }}" type="text/javascript" src="/static/js/pubsub/subscription.js"></script>

    <script nonce="{{ CSP_NONCE }}" type="text/javascript">

        window.zato_select_data_source_id = 'id_endpoint_type';
        window.zato_select_data_target_id = 'id_endpoint_id';
        window.zato_select_data_target_items = {
            'id_endpoint_id': {
                'rest': {{ select_data_target.rest|safe }},
                'srv': {{ select_data_target.srv|safe }},
            }
        }

        </script>

    <script nonce="{{ CSP_NONCE }}" type="text/javascript">
    $.fn.zato.data_table.get_columns = function() {
        return [

            '_numbering',
            '_selection',

            'is_active',
            'endpoint_name',
            '_endpoint_type_name',

            'role',
            'subscription_count',

            // '_last_pub_time',
            // '_last_seen',
            // '_last_deliv_time',

            '_delete',
            'id',

            'endpoint_type',
        ]
    }
    </script>

{% endblock %}

{% block "content" %}
<h2 class="zato">Pub/sub subscriptions{% if topic_id %} for topic <a href="{% url "pubsub-topic" %}?cluster={{ cluster_id }}&amp;query={{ topic_name }}">{{ topic_name }}{% endif %}</a></h2>

{% if not zato_clusters %}
    {% include "zato/no-clusters.html" %}
{% else %}

    <div id="user-message-div" style='display:none'><pre id="user-message" class="user-message"></pre></div>

{% include "zato/search-form.html" with page_prompt="Show subscriptions"%}

    {% if cluster_id %}

        {% if not topic_id %}
        <div class='page_prompt' style="clear:left">
            <a href="javascript:$.fn.zato.pubsub.subscription.create()">Create pub/sub subscriptions</a>
        </div>
        {% endif %}

        <div id="markup">
            <table id="data-table">
                <thead>
                    <tr class='ignore'>

                        <th>&nbsp;</th>
                        <th>&nbsp;</th>

                        <th><a href="#">Active</a></th>
                        <th style="width:10%"><a href="#">Endpoint</a></th>
                        <th><a href="#">Type</a></th>

                        <th><a href="#">Role</a></th>
                        <th><a href="#"># of subscriptions</a></th>

                        <!--
                        <th><a href="#">Last pub. time</a></th>
                        <th><a href="#">Last seen</a></th>
                        <th><a href="#">Last deliv. time</a></th>
                        -->

                        <th>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>

                        <th class='ignore'>&nbsp;</th>

                </thead>

                <tbody>
                {% if items %}
                {% for item in items %}
                    <tr class="{% cycle 'odd' 'even' %}" id='tr_{{ item.id }}'>

                        <td class='numbering'>&nbsp;</td>
                        <td class="impexp"><input type="checkbox" /></td>

                        <td>{{ item.is_active|yesno:"Yes,No" }}</td>
                        <td><a href="{% url "pubsub-endpoint" %}?cluster={{ cluster_id }}&amp;query={{ item.endpoint_name }}">{{ item.endpoint_name }}</a></td>
                        <td>{{ item.endpoint_type_name }}</td>

                        <td>{{ item.role }}</td>
                        <td>
                            <a id="pubsub_endpoint_queues_link_{{ item.id }}"
                               href="{% url "pubsub-endpoint-queues" cluster_id item.id item.endpoint_name|slugify %}?cluster={{ cluster_id }}">{{ item.subscription_count}} </a>
                        </td>

                        <!--
                        <td>{{ item.last_pub_time|default:"<span class='form_hint'>---</span>" }}</td>
                        <td>{{ item.last_seen|default:"<span class='form_hint'>---</span>" }}</td>
                        <td>{{ item.last_deliv_time|default:"<span class='form_hint'>---</span>" }}</td>
                        -->

                        <td>
                            {% if not topic_id %}
                                {% if item.is_internal and item.endpoint_type == "internal" %}
                                    <span class="form_hint">Delete all subscriptions</span>
                                {% else %}
                                    <a href="javascript:$.fn.zato.pubsub.subscription.delete_('{{ item.id }}')">Delete all subscriptions</a>
                                {% endif %}
                            {% endif %}
                        </td>
                        <td class='ignore item_id_{{ item.id }}'>{{ item.id }}</td>

                        <td class='ignore'>{{ item.endpoint_type }}</td>

                    </tr>
                {% endfor %}
                {% else %}
                    <tr class='ignore'>
                        <td colspan='11'>No results</td>
                    </tr>
                {% endif %}

                </tbody>
            </table>
        </div>

{% include "zato/paginate/action-panel.html" %}

        <form novalidate id="topic_search_form"></form>

        <div id="create-div" class='data-popup ignore'>
            <div class="bd">
                <form novalidate action="{% url "pubsub-subscription-create" %}" method="post" id="create-form">
                    <table class="form-data">

                        <tr>
                            <td style="vertical-align:middle; width:20%">Type</td>
                            <td>
                                {{ create_form.endpoint_type }}
                                | Delivery server {{ create_form.server_id }}
                            </td>
                        </tr>
                        <tr>
                            <td class="zato-toggle zato-toggle-rest zato-toggle-srv zato-toggle-visible" style="vertical-align:middle; width:12%">Endpoint</td>
                            <td>
                                {{ create_form.endpoint_id }}
                            </td>
                        </tr>

                        {% comment %} --------- REST --------- {% endcomment %}
                        <tr id="tr_toggle_out_rest_http_soap_id" class="zato-toggle zato-toggle-rest zato-toggle-visible">
                            <td style="vertical-align:middle; width:12%">REST connection</td>
                            <td>{{ create_form.out_rest_http_soap_id }}</td>
                        </tr>

                        {% comment %} --------- REST --------- {% endcomment %}
                        <tr id="tr_toggle_out_http_method" class="zato-toggle zato-toggle-rest zato-toggle-visible">
                            <td style="vertical-align:middle; width:12%">REST method</td>
                            <td>{{ create_form.out_http_method }}</td>
                        </tr>

                        <!--
                        {% comment %} --------- Service --------- {% endcomment %}
                        <tr id="tr_toggle_service_id" class="zato-toggle zato-toggle-srv zato-toggle-hidden">
                            <td style="vertical-align:middle">Service</td>
                            <td>{{ create_form.service_id }} </td>
                        </tr>
                        -->

                        <tr>
                            <td style="vertical-align:middle; width:12%">Delivery method</td>
                            <td>
                                {{ create_form.delivery_method }}
                            </td>
                        </tr>

                        <tr>
                            <td style="vertical-align:middle; width:12%">Deliv. batch size</td>
                            <td>
                                {{ create_form.delivery_batch_size }}
                                |
                                As list
                                {{ create_form.wrap_one_msg_in_list }}
                                |
                                Deliv. max retries
                                {{ create_form.delivery_max_retry }}
                            </td>
                        </tr>

                        <tr>
                            <td style="vertical-align:middle; width:12%">
                                Sleep on error
                                <br/>
                                socket / other
                            </td>
                            <td>
                                {{ create_form.wait_sock_err }}
                                {{ create_form.wait_non_sock_err }}
                                <span class="form_hint">(both in seconds)</span>
                            </td>
                        </tr>

                        {% comment %} --------- Topics --------- {% endcomment %}
                        <tr>
                            <td style="vertical-align:middle">
                                Topics

                                <!--
                                <br/>
                                <input form="topic_search_form" type="text" name="topic_filter_by" id="topic_filter_by_id" style="width:100px;font-size:11px"/>
                                <br/>
                                <input form="topic_search_form" type="button" value="Filter" style="margin-top:4px; float:left" />
                                <div style="display:block; padding-top:8px;">
                                    &nbsp;<a href="javascript:document.getElementById('topic_search_form').reset();">Clear</a>
                                </div>
                                -->
                            </td>
                            <td id="td_topic_list">

                                <div class="multi-select-div" id="multi-select-div">
                                    <input class="multi-select-input" id="multi-select-input" disabled="disabled"></input>
                                </div>

                            </td>
                        </tr>

                        <tr>
                            <td colspan="2" style="text-align:right">
                                <input type="submit" value="OK" />
                                <button type='button' onclick='javascript:$.fn.zato.data_table.close(this)'>Cancel</button>
                            </td>
                        </tr>

                    </table>
                    <input type="hidden" name="is_active" value="on" />
                    <input type="hidden" id="cluster_id" name="cluster_id" value="{{ cluster_id }}" />
                    <input type="hidden" id="out_job_id" name="out_job_id" />
                </form>
            </div>
        </div>

    {% endif %} {% comment %}cluster_id{% endcomment %}

{% endif %}{% comment %}not zato_clusters{% endcomment %}

{% endblock %}
